package com.ash.util;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author : Chen Chao
 * @encoding : UTF-8
 * @date : 2021/3/16 15:50
 * @file : project03.com.ash.Utils.JsonUtils.java
 * @software : IntelliJ IDEA
 * @since : 1.8
 */
public class JsonUtil {
    public static String getJsonByObject(Object obj, String pattern){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        objectMapper.setDateFormat(format);
        String s=null;
        try {
            s = objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return s;
    }
    public static String getJsonByObject(Object obj){
        return getJsonByObject(obj,"yy-MM--dd:hh:mm:ss");
    }
    public  static String getJsonList(int count, List<?> list){
        /*封装数据并返回*/
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("data", list);
        jsonObject.put("count", count);
        return jsonObject.toJSONString();

    }
}
